LINE_SEP = '\n'
STATE_ON = 'wlaczony'
STATE_OFF = 'wylaczony'
MSG_TEMPL = '%s: %s'

def state_to_text(name, state):
    state_text = 'wlaczony' if state else 'wylaczony'
    service_text = '%s: %s' % (name, state_text)
    return service_text

class TextStatusUpdater:
    
    def __init__(self, update_cb):
        self.state_lines = {}
        self.service_names = []
        self.update_cb = update_cb
        
    def set_state_line(self, name, state):
        self.state_lines[name] = state_to_text(name, state)
        self.update_text()
        
    def update_text(self):
        lines = [self.state_lines[n] for n in self.service_names]
        text = LINE_SEP.join(lines)
        self.update_cb(text)
        
    def get_state_cb(self, name):
        def f(state):
            self.set_state_line(name, state)
        return f
    
    def register_service(self, name, service):
        self.service_names.append(name)
        service.state_cb = self.get_state_cb(name)
        self.set_state_line(name, service.state)